package com.yellow.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yellow.constant.MessageConstant;
import com.yellow.enity.Result;
import com.yellow.pojo.OrderSetting;
import com.yellow.pojo.OrderSettingVo;
import com.yellow.service.OrderSettingService;
import com.yellow.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            POIUtils.checkFile(excelFile);
            List<String[]> list = POIUtils.readExcel(excelFile);
            //将list转换为ordersetting
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strs : list) {
                //获取时间日期
                String date = strs[0];
                String number = strs[1];
                orderSettingList.add(new OrderSetting(new Date(date), Integer.parseInt(number)));
            }
            orderSettingService.upload(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    //{ date: 5, number: 120, reservations: 1 },
    //{ date: 15, number: 120, reservations: 1 },

    //List<list<map>>
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//2019-11
        try {
//            System.out.println("date = " + date);
            List<OrderSettingVo> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @RequestMapping("/addOrderNumber")
    public Result addOrderNumber(String date, int number) {
        try {
            orderSettingService.addOrderNumber(date, number);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

    }
}
